package com.easylinkin.linkappapi.openapi.service.impl;

import com.easylinkin.linkappapi.openapi.service.ApplicationCopyDirectService;
import com.easylinkin.linkappapi.security.entity.LinkappPrivilege;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.security.service.LinkappUserService;
import com.easylinkin.linkappapi.tenant.entity.TenantRefPrivilege;
import com.easylinkin.linkappapi.tenant.mapper.LinkappTenantMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: 菜单权限拷贝
 * @author: chenkaixuan
 * @create: 2021-09-06 15:39
 */
@Slf4j
@Order(1)
@Service
public class ApplicationCopyMenuDirectServiceImpl implements ApplicationCopyDirectService {
    @Resource
    LinkappTenantMapper linkappTenantMapper;

    @Override
    public RestMessage copy(HashMap<String,String> screenmap,HashMap<String,String> map,LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
        List<LinkappPrivilege> privileges = new ArrayList<>();
        //菜单
        List<LinkappPrivilege> linkappPrivileges = linkappTenantMapper.selectPrivilegeByTenant(oldLinkappUser.getTenantId(), null);
        if (linkappPrivileges != null && linkappPrivileges.size() > 0) {
            privileges.addAll(linkappPrivileges);
        }
        //小程序菜单
        linkappPrivileges = linkappTenantMapper.selectPrivilegeByTenant(oldLinkappUser.getTenantId(), 1);
        if (linkappPrivileges != null && linkappPrivileges.size() > 0) {
            privileges.addAll(linkappPrivileges);
        }
        //批量新增
        int batchSize = 100;
        if (privileges != null && privileges.size() > 0) {
            List<TenantRefPrivilege> collect = privileges.stream().map(m -> {
                TenantRefPrivilege tenantRefPrivilege = new TenantRefPrivilege();
                tenantRefPrivilege.setTenantId(newLinkappUser.getTenantId());
                tenantRefPrivilege.setPrivilegeId(m.getId().toString());
                tenantRefPrivilege.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                return tenantRefPrivilege;
            }).collect(Collectors.toList());
            for (int idx = 0; idx < collect.size(); idx += batchSize) {
                List<TenantRefPrivilege> list = collect.subList(idx, Math.min(collect.size(), idx + batchSize));
                linkappTenantMapper.batchInsert(list);
            }
        }
        log.info("菜单权限拷贝完成");
        return restMessage;
    }
}
